#自建文件打开方法--系统调用
'''
1.可以通过__exit__返回异常，只需要在__exit__方法内return True， 通过try方法捕获
'''

import os


class File:
    def __init__(self, path):
        self.fd = os.open(path, os.O_RDONLY)

    def read(self, size):
        return os.read(self.fd, size)

    def close(self):
        os.close(self.fd)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()


def open(path):
    return File(path)


with open('./decrator.py') as fp:
    print(fp.read(100).decode('utf-8'))


